// 静态属性，样式属性，剥离数据
import 'echarts/map/js/china'
import 'echarts/map/js/province/anhui'
import 'echarts/map/js/province/aomen'
import 'echarts/map/js/province/beijing'
import 'echarts/map/js/province/chongqing'
import 'echarts/map/js/province/fujian'
import 'echarts/map/js/province/gansu'
import 'echarts/map/js/province/guangdong'
import 'echarts/map/js/province/guangxi'
import 'echarts/map/js/province/guizhou'
import 'echarts/map/js/province/hainan'
import 'echarts/map/js/province/hebei'
import 'echarts/map/js/province/heilongjiang'
import 'echarts/map/js/province/henan'
import 'echarts/map/js/province/hubei'
import 'echarts/map/js/province/hunan'
import 'echarts/map/js/province/jiangsu'
import 'echarts/map/js/province/jiangxi'
import 'echarts/map/js/province/jilin'
import 'echarts/map/js/province/liaoning'
import 'echarts/map/js/province/neimenggu'
import 'echarts/map/js/province/ningxia'
import 'echarts/map/js/province/qinghai'
import 'echarts/map/js/province/shandong'
import 'echarts/map/js/province/shanghai'
import 'echarts/map/js/province/shanxi'
import 'echarts/map/js/province/shanxi1'
import 'echarts/map/js/province/sichuan'
import 'echarts/map/js/province/taiwan'
import 'echarts/map/js/province/tianjin'
import 'echarts/map/js/province/xianggang'
import 'echarts/map/js/province/xinjiang'
import 'echarts/map/js/province/xizang'
import 'echarts/map/js/province/yunnan'
import 'echarts/map/js/province/zhejiang'
import chinaCities from '../echarts/chinaCities'

const geoCoordMap = chinaCities.chinaCities
let convertData = function (data) {
  // 计算散点图位置的函数
  let res = []
  for (let i = 0; i < data.length; i++) {
    let geoCoord = geoCoordMap[data[i].name]
    if (geoCoord) {
      res.push({
        name: data[i].name,
        value: geoCoord.concat(data[i].value)
      })
    }
  }
  return res
}

let areaData = function (data) {
  let res = []
  for (let i = 0; i < data.length; i++) {
    res.push({
      name: data[i].name,
      value: data[i].value
    })
  }
  return res
}
const staticOption = {
  toolbox: {
    show: true,
    top: 0,
    right: 0,
    feature: {
      restore: {
        show: true,
        title: '返回'
      }
    }
  },
  geo: {
    show: true,
    map: 'china',
    selectedMode: 'single',
    roam: true,
    z: 4,
    label: {
      normal: {
        show: false,
        color: '#bbb',
        position: 'top'
      },
      emphasis: {
        show: true,
        color: '#bbb',
        position: 'top'
      }
    },
    itemStyle: {
      normal: {
        areaColor: 'rgba(26,124,230,0.1)',
        borderColor: '#1a7ce6',
        borderWidth: 1
      },
      emphasis: {
        areaColor: 'rgba(26,124,230,0.5)'
      }
    }
  },
  visualMap: {
    min: 1,
    max: 50,
    text: ['High', 'Low'],
    realtime: false,
    calculable: true,
    inRange: {
      color: ['transparent', 'yellow', 'orangered']
    }
  },
  series: [
    {
      name: 'mapItem',
      type: 'map',
      roam: true,
      mapType: 'china',
      z: 2,
      zlevel: 1,
      selectedMode: true,
      geoIndex: 0,
      hoverAnimation: true,
      label: {
        normal: {
          show: true,
          color: '#bbb',
          position: 'top'
        },
        emphasis: {
          show: true,
          color: '#bbb',
          position: 'top'
        }
      },
      itemStyle: {
        areaColor: 'rgba(26,124,230,0.1)',
        borderColor: '#1a7ce6',
        borderWidth: 1
      },
      data: []
    },
    {
      name: '',
      type: 'effectScatter',
      coordinateSystem: 'geo',
      hoverAnimation: true,
      effectType: 'ripple',
      z: 5,
      // symbolOffset: ['20%', '120%'],
      rippleEffect: {
        period: 4,
        scale: 3,
        brushType: 'stroke'
      },
      symbolSize: 10,
      data: [],
      label: {
        normal: {
          show: false
        },
        emphasis: {
          show: false
        }
      },
      itemStyle: {
        normal: {
          color: '#1a7ce6'
        },
        emphasis: {
          show: true
        }
      }
    }
  ]
}

class ChartFactory {
  constructor (additionalOptions) {
    // 下方复制属于浅拷贝，指向同个内存，针对可能有更改的需求，要进行一次清理
    this.option = staticOption
    // 装配数据
    if (additionalOptions.map) {
      console.log(additionalOptions.map)
      // this.option.geo.map = additionalOptions.map
      this.option.geo.map = 'china'
    }
    this.clear()
    this.option.series[0].data = areaData(additionalOptions['areaData'])
    this.option.series[1].data = convertData(additionalOptions['scatterData'])
  }

  // 清理方法，要将部署的新数据全部置空
  clear () {
    this.option.series[0].data = []
    this.option.series[1].data = []
  }
  getOption () {
    return this.option
  }
}

export { ChartFactory }
